package main

import (
	"encoding/json"
	"fmt"
	"log"
	"sort"
)

type Data struct {
	Id        int `json:"id"`
	FirstSort int `json:"first_sort"`
	SecSort   int `json:"sec_sort"`
}

type List1 []Data

func main() {
	//data := List1{{1, 1, 3}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 9999999}, {5, 5, 2}, {6, 6, 4}, {7, 7, 9999999}, {8, 8, 9999999}} //2,5,1,6,3,4,7,8
	//data := List1{{1, 1, 8}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 9999999}, {5, 5, 2}, {6, 6, 1000}, {7, 7, 9999999}, {8, 8, 9999999}} //2,5,3,4,7,8,1,6
	//data := List1{{1, 1, 8}, {2, 2, 2}, {3, 3, 1}, {4, 4, 3}, {5, 5, 4}, {6, 6, 5}, {7, 7, 7}, {8, 8, 6}} //3,2,4,5,6,8,7,1
	//data := List1{{1, 1, 8}, {2, 2, 2}, {3, 3, 1}, {4, 4, 3}, {5, 5, 1000}, {6, 6, 5}, {7, 7, 7}, {8, 8, 6}} //3,2,4,6,8,7,1,5
	//data := List1{{1, 1, 3}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 1}, {5, 5, 2}, {6, 6, 4}, {7, 7, 9999999}, {8, 8, 9999999}} //4,5,1,6,2,3,7,8
	data := List1{{1, 1, 9999999}, {2, 2, 9999999}, {3, 3, 9999999}, {4, 4, 7}, {5, 5, 9999999}, {6, 6, 9999999}, {7, 7, 9999999}, {8, 8, 9999999}} //1,2,3,5,6,7,4,8

	data1 := []*Data{&Data{11, 1, 2}}
	bytes, _ := json.Marshal(data1)
	log.Println(string(bytes))

	var (
		posTmp   = []Data{}
		otherTmp = []Data{}
	)

	for _, item := range data {
		if item.SecSort != 9999999 {
			posTmp = append(posTmp, item)
			continue
		}
		otherTmp = append(otherTmp, item)
	}

	sort.SliceStable(posTmp, func(i, j int) bool {
		return posTmp[i].SecSort < posTmp[j].SecSort
	})

	for _, v := range posTmp {
		pos := v.SecSort - 1
		otherTmp = appendPos(otherTmp, pos, v)
	}
	fmt.Println(otherTmp)
}

func appendPos(list []Data, index int, item Data) []Data {
	if index < 0 {
		index = 0
	}
	if index > len(list) {
		return append(list, item)
	}
	if len(list) == index {
		return append(list, item)
	}

	list = append(list[:index+1], list[index:]...)
	list[index] = item
	return list
}
